<?php
namespace app\api\controller\v1\controller\pet;

use app\api\controller\v1\model\pet\Feed as Fe;
use think\Controller;
use app\api\controller\Api;
use think\Model;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;


class Feed extends api
{

//喂养宠物
    public function pet_hungry_level($user_id,$be_user_id,$pet_id,$food_count){

        $time = date('Y-m-d H:i:s');
        $food = Db::query("select pet_food from ym_pet_food where user_id = '$user_id'");
        $pet_food = $food[0]['pet_food'];
        if ($pet_food < $food_count) {
            return $this->returnmsg(402,'宠粮余额不足,请充值');
        }else{
            $hungry = Db::query("select pet_hungry_level 
                                    from ym_pet_hungry 
                                    where pet_id = '$pet_id'");
            $hungry_level = $hungry[0]['pet_hungry_level'];

            Db::query("update ym_pet_hungry set pet_hungry_level = '$hungry_level' + '$food_count' where pet_id = '$pet_id'");
            Db::query("update ym_pet_food set pet_food = '$pet_food' - '$food_count' where user_id = '$user_id'");
            $be_food = Db::query("select pet_food from ym_pet_food where user_id = '$be_user_id'");
            $be_pet_food = $be_food[0]['pet_food'];
            Db::query("update ym_pet_food set pet_food = '$be_pet_food' + '$food_count' where user_id = '$be_user_id'");

            // message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>9,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $img = Db::query("select pet_icon from ym_pet where pet_id = '$pet_id' limit 1");
            $img_url = $img[0]['pet_icon'];
            $reward_data = ['m_id'=>$m_id,'module_type'=>2,'id'=>$pet_id,'food_count'=>$food_count,'img_url'=>$img_url];
            $m_reward_id = Db::table('ym_message_reward')->insertGetId($reward_data);
            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];

            //推送
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'喂养了你的宠物'.$food_count.'克宠粮');
            return $this->returnmsg(200,'喂养成功');
        }
    }



    //定时减去百分之四十饥饿值11-29
    public function less_hungry_level(){
        $result = Db::query("select pet_hungry_level from ym_pet_hungry");
        $length = count($result);
        for ($i=0; $i < $length ; $i++) {
            // $pet_hungry_level = ;
            Db::query("update ym_pet_hungry set pet_hungry_level = '".$result[$i]['pet_hungry_level']."' *0.6 where pet_hungry_level = '".$result[$i]['pet_hungry_level']."' ");
        }
        return $this->returnmsg(200,'饥饿值已经减去40%');
    }


    //拿到该用户的宠粮数
    public function get_pet_food($user_id){
        $result = Db::query("select pet_food from ym_pet_food where user_id = '$user_id'");
        $pet_food = $result[0]['pet_food'];
        return $pet_food;
    }



//打赏-打赏者部分
    public function user_reward($reward_user_id,$bereward_user_id,$food_count,$id,$type){
        // $result = Db::query("select pet_food from user_info where user_id = '$reward_user_id'");
        // $pet_food = $result[0]['pet_food'];
        $time = date('Y-m-d H:i:s');
        $pet_food = $this->get_pet_food($reward_user_id);
        if ($pet_food >= $food_count) {
            Db::query("update ym_pet_food set pet_food = '$pet_food' - '$food_count' where user_id = '$reward_user_id'");
            $message_data = ['be_user_id'=>$bereward_user_id,'user_id'=>$reward_user_id,'message_type'=>5,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            if ($type == 0) {//随手拍
                $post_id = $id;
                $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id' limit 1");
                $img_url = $img[0]['img_url'];
                $reward_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'food_count'=>$food_count,'img_url'=>$img_url];
                $m_reward_id = Db::table('ym_message_reward')->insertGetId($reward_data);
                // Db::query("insert into ym_message_reward_post values(null,'$m_reward_id','$post_id','$food_count','$img_url')");
            }elseif ($type == 1) {//个人动态
                $dynamic_id = $id;
                $img = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id' limit 1");
                $img_url = $img[0]['img_url'];
                $reward_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'food_count'=>$food_count,'img_url'=>$img_url];
                $m_reward_id = Db::table('ym_message_reward')->insertGetId($reward_data);
            }

            $user_name = Db::query("select user_name from user_info where user_id = '$reward_user_id'");
            $name = $user_name[0]['user_name'];

            // $message = '关注了你';
            $bereward_user_id = strval($bereward_user_id);
            $this->push($bereward_user_id,$name.'打赏了你'.$food_count.'克宠粮');
            $this->user_be_rewarded($bereward_user_id,$food_count);
        }else{
            return $this->returnmsg(402,'宠粮余额不足,请充值');
        }
    }



    //打赏-被打赏更新宠粮
    public function user_be_rewarded($bereward_user_id,$food_count){

        $pet_food = $this->get_pet_food($bereward_user_id);

        Db::query("update ym_pet_food set pet_food = '$pet_food' + '$food_count' where user_id = '$bereward_user_id'");
        return $this->returnmsg(200,"打赏成功");
    }



















//推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }




}